package com.wavecoler.professor.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import com.wavecoler.professor.dao.TeamDAO;
import com.wavecoler.professor.objects.Team;
import com.wavecoler.professor.objects.User;

@SuppressWarnings("serial")
public class RegisterGroupServlet extends HttpServlet {

	private List<String> participants;

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		participants = new ArrayList<String>();

		String groupName = request.getParameter("groupname");
		String parts[] = request.getParameterValues("participants");

		for(int i = 0; i < parts.length; i++) {
			participants.add(parts[i].trim());
		}
        /*******************************************
         * es necesario procedimiento para leer usuarios antes de asignarlos en lugar de poner null */
        ArrayList<User> users = null;
        
		Team g = new Team();
		g.setTeamName(groupName);
		g.setUsers(users);
		TeamDAO.insertTeam(g);

		String address = "jsps/ShowGroups.jsp";
		RequestDispatcher dispatcher = request.getRequestDispatcher(address);
		dispatcher.forward(request, response);

	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
}
